package com.cloud.wms.app;

import android.content.Context;
import android.text.TextUtils;

import androidx.multidex.MultiDex;

import com.cloud.core.app.BaseApplication;
import com.cloud.core.multistate.EmptyAdapter;
import com.cloud.core.multistate.ErrorAdapter;
import com.cloud.core.multistate.LoadingAdapter;
import com.cloud.core.widget.header.DeliveryHeader;
import com.cloud.wms.BuildConfig;
import com.cloud.wms.R;
import com.cloud.wms.config.SpKey;
import com.cloud.wms.injection.components.ApplicationComponent;
import com.cloud.wms.injection.components.DaggerApplicationComponent;
import com.cloud.wms.injection.modules.ApplicationModule;
import com.cloud.wms.ui.main.activity.LoginActivity;
import com.cloud.wms.ui.main.entity.UserEntity;
import com.dylanc.loadinghelper.LoadingHelper;
import com.dylanc.loadinghelper.ViewType;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mmkv.MMKV;

import kotlin.Unit;


/**
 * Created by Tanghaibo
 * 2018/3/21
 * description: Application类
 */

public class FineExApplication extends BaseApplication {
    private static ApplicationComponent mApplicationComponent;

    private static FineExApplication mApplication;

    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            return new ClassicsHeader(context);
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator((context, layout) -> {
            //指定为经典Footer，默认是 BallPulseFooter
            return new ClassicsFooter(context).setDrawableSize(20);
        });
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mApplication = this;
        setupDaggerComponent();

        MMKV.initialize(this);
        //异常捕捉类
        mApplicationComponent.crash();
        initBugly();

        LoadingHelper.setDefaultAdapterPool(adapterPool -> {
            adapterPool.register(ViewType.LOADING, new LoadingAdapter());
            adapterPool.register(ViewType.ERROR, new ErrorAdapter());
            adapterPool.register(ViewType.EMPTY, new EmptyAdapter());
            return Unit.INSTANCE;
        });

        if (!TextUtils.isEmpty(FineExApplication.component().kv().getString(SpKey.PRINT_DEVICE, ""))) {
            FineExApplication.component().printer().initPrinter(FineExApplication.component().kv().getString(SpKey.PRINT_DEVICE, ""));
        }
    }

    /**
     * 注入applicationComponent
     */
    private void setupDaggerComponent() {
        mApplicationComponent = DaggerApplicationComponent.builder()
                .applicationModule(new ApplicationModule(mApplication))
                .build();
        mApplicationComponent.inject(this);
    }

    /**
     * 初始化bugly
     */
    private void initBugly() {
        // bugly的ID
        CrashReport.initCrashReport(getApplicationContext(), "59a518e8a3", false);
        CrashReport.setIsDevelopmentDevice(getApplicationContext(), BuildConfig.DEBUG);
        UserEntity userInfo = FineExApplication.component().loginCache().getUserInfo();
        if (userInfo != null) {
            String userName = userInfo.getUserName();
            CrashReport.setUserId(userName);
        }
    }

    public synchronized static FineExApplication getInstance() {
        return mApplication;
    }

    public static ApplicationComponent component() {
        return mApplicationComponent;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //方法数超过分包
        MultiDex.install(this);
    }
}
